package com.yl.web.controller.api;

import com.yl.common.annotation.Anonymous;
import com.yl.common.core.domain.AjaxResult;
import com.yl.domain.ConferenceDO;
import com.yl.domain.dto.ShengWangDTO;
import com.yl.service.ShengWangService;
import com.yl.utils.CommonUtils;
import com.yl.utils.RtcTokenBuilder2Sample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

@RequestMapping("/sw/")
@RestController
@Anonymous
public class ShengWangController {

    private static final Logger log = LoggerFactory.getLogger(ShengWangController.class);

    @Resource
    ShengWangService shengWangService;



    @RequestMapping("start")
    @ResponseBody
    public AjaxResult start(@RequestBody ShengWangDTO shengWangDTO) {
        shengWangDTO.setUid(CommonUtils.get10DigitTimestamp() + "");
        String swToken = RtcTokenBuilder2Sample.getSwToken(shengWangDTO.getCname(), shengWangDTO.getUid());
        log.info("开始录制=Controller==学生id- {}  开始录制使用的uid - {} ===参数 - {}", shengWangDTO.getUid(), shengWangDTO.getUid(), shengWangDTO);
        shengWangDTO.setUserToken(swToken);
        AjaxResult result = shengWangService.start(shengWangDTO.getCname(), shengWangDTO.getUid(), shengWangDTO.getUserToken());
        return result;
    }

    @RequestMapping("query")
    @ResponseBody
    public AjaxResult query(@RequestBody ShengWangDTO shengWangDTO) {
        log.info("查询录制状态======参数={}", shengWangDTO);
        AjaxResult result = shengWangService.query(shengWangDTO.getResourceid(), shengWangDTO.getSid());
        return result;
    }


    @RequestMapping("stop")
    @ResponseBody
    public AjaxResult stop(@RequestBody ShengWangDTO shengWangDTO) {
        log.info("停止录制======参数={}", shengWangDTO);
        String result = shengWangService.stop(shengWangDTO.getCname(),
                shengWangDTO.getUid(),
                shengWangDTO.getResourceid(),
                shengWangDTO.getSid());
        return AjaxResult.success(result);
    }

    @RequestMapping("getSwToken")
    @ResponseBody
    public AjaxResult getSwToken(@RequestBody ShengWangDTO shengWangDTO) {
        log.info("获取Token======参数={}", shengWangDTO);
        String swToken = RtcTokenBuilder2Sample.getSwToken(shengWangDTO.getCname(), shengWangDTO.getUid());
        HashMap<String, Object> map = new HashMap<>(16);
        map.put("token", swToken);
        return AjaxResult.success(map);
    }
}
